package com.wyzz.hr.action;

import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.bo.IAttenddailyBO;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.IEmpTypeBO;
import com.wyzz.hr.bo.ILocationBO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;

public class SearchAttenddaily extends BaseDownloadAction {

	public SearchAttenddaily() {
		page = new Pager();
		searchOrExport = null;
		docStream = null;
		contentDisposition = null;
		serverFileName = null;
		searchMode = DatabaseSysConfigManager.getInstance().getProperty(
				"sys.examin.hourtoday.mode");
		attenddailyBO = (IAttenddailyBO) getBean("attenddailyBO");
	}

	public String execute() throws Exception {
		getDrillDownList();
		execParameter();
		List empList = searchEmp();
		if ("export".equals(searchOrExport)) {
			setSearchOrExport("");
			List results = attenddailyBO.searchAttenddaily(null, emp, employee,
					requireDateFrom, requireDateTo, searchType, true, empList);
			if (results == null || results.size() == 0) {
				addActionError("\u65E0\u6BCF\u65E5\u6570\u636E\u53EF\u4EE5\u5BFC\u51FA\uFF01");
				return "success";
			} else {
				return downloadToExcel(results, "OAttendDaily",
						outmatchModelId, "empObj");
			}
		} else {
			attenddailyList = attenddailyBO.searchAttenddaily(page, emp,
					employee, requireDateFrom, requireDateTo, searchType,
					false, empList);
			return "success";
		}
	}

	private void execParameter() {
		if (employee == null)
			employee = new Employee();
		String dateReg = "^\\d{4}-\\d{1,2}-\\d{1,2}$";
		if (StringUtils.isEmpty(attendDate) || !attendDate.matches(dateReg))
			attendDate = DateUtil.formatDateToS(new Date(), "yyyy-MM-dd");
		if (StringUtils.isEmpty(attendDateTo) || !attendDateTo.matches(dateReg))
			attendDateTo = DateUtil.formatDateToS(new Date(), "yyyy-MM-dd");
		requireDateFrom = DateUtil.parseDateByFormat(attendDate, "yyyy-MM-dd");
		requireDateTo = DateUtil.parseDateByFormat(attendDateTo, "yyyy-MM-dd");
	}

	private List searchEmp() {
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class, "emp");
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 4);
		if ("OWN".equals(super.authorityCondition))
			BaseCrit.addDC(dc, Employee.PROP_ID, "eq",
					new String[] { getCurrentEmpNo() });
		addCriteria(dc);
		return attenddailyBO.searchEmployee(dc);
	}

	private void addCriteria(DetachedCriteria dc) {
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empOrgDept", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), "empOrgLoc", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_TYPE).toString(), "empType", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_PB_NO).toString(), "empPbNo", 1);
		BaseCrit.addEmpDC(dc, "emp", emp);
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), Location.PROP_ID,
				new Object[] { employee.getEmpLocationNo() });
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, employee.getEmpDeptNo());
		dc.add(Restrictions.le((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_JOIN_DATE).toString(), requireDateTo));
		dc.add(Restrictions.or(Restrictions.eq((new StringBuilder()).append(
				"emp.").append(Employee.PROP_EMP_STATUS).toString(), Integer
				.valueOf(1)), Restrictions.and(Restrictions.eq(
				(new StringBuilder()).append("emp.").append(
						Employee.PROP_EMP_STATUS).toString(), Integer
						.valueOf(0)), Restrictions.ge((new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_TERMINATE_DATE)
				.toString(), requireDateFrom))));
	}

	private void getDrillDownList() {
		ILocationBO localbo = (ILocationBO) SpringBeanFactory
				.getBean("locationBO");
		locations = localbo.FindEnabledLocation();
		IEmpTypeBO emptypebo = (IEmpTypeBO) SpringBeanFactory
				.getBean("emptypeBO");
		empType = emptypebo.FindEnabledEmpType();
		IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		departs = deptbo.FindEnabledDepartment();
	}

	public String getEmpType(String typeId) {
		if (typeId == null)
			return "";
		for (Iterator iterator = empType.iterator(); iterator.hasNext();) {
			Emptype type = (Emptype) iterator.next();
			if (typeId.equals(type.getId()))
				return type.getEmptypeName();
		}

		return "";
	}

	public String getLocName(String id) {
		if (id == null || id == "")
			return "";
		for (Iterator iterator = locations.iterator(); iterator.hasNext();) {
			Location loc = (Location) iterator.next();
			if (id.equals(loc.getId()))
				return loc.getLocationName();
		}

		return "";
	}

	public String getDepName(String id) {
		if (id == null || id == "")
			return "";
		for (Iterator iterator = departs.iterator(); iterator.hasNext();) {
			Department dep = (Department) iterator.next();
			if (id.equals(dep.getId()))
				return dep.getDepartmentName();
		}

		return "";
	}

	public String formatBD(BigDecimal input) {
		if (input == null) {
			return null;
		} else {
			DecimalFormat df = new DecimalFormat("#0.0 ");
			return df.format(input);
		}
	}

	public String formatBD_m(BigDecimal input) {
		if (input == null) {
			return null;
		} else {
			DecimalFormat df = new DecimalFormat("#0 ");
			return df.format(input);
		}
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public List getAttenddailyList() {
		return attenddailyList;
	}

	public void setAttenddailyList(List attenddailyList) {
		this.attenddailyList = attenddailyList;
	}

	public String getEmp() {
		return emp;
	}

	public void setEmp(String emp) {
		this.emp = emp;
	}

	public List getDeparts() {
		return departs;
	}

	public void setDeparts(List departs) {
		this.departs = departs;
	}

	public List getLocations() {
		return locations;
	}

	public void setLocations(List locations) {
		this.locations = locations;
	}

	public List getEmpType() {
		return empType;
	}

	public void setEmpType(List empType) {
		this.empType = empType;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getAttendDate() {
		return attendDate;
	}

	public void setAttendDate(String attendDate) {
		this.attendDate = attendDate;
	}

	public Integer getSearchType() {
		return searchType;
	}

	public void setSearchType(Integer searchType) {
		this.searchType = searchType;
	}

	public String getAttendDateTo() {
		return attendDateTo;
	}

	public void setAttendDateTo(String attendDateTo) {
		this.attendDateTo = attendDateTo;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public FileInputStream getDocStream() {
		return docStream;
	}

	public void setDocStream(FileInputStream docStream) {
		this.docStream = docStream;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public String getServerFileName() {
		return serverFileName;
	}

	public void setServerFileName(String serverFileName) {
		this.serverFileName = serverFileName;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public static String getOutputIoName() {
		return "OAttendDaily";
	}

	public String getSearchMode() {
		return searchMode;
	}

	public void setSearchMode(String searchMode) {
		this.searchMode = searchMode;
	}

	private static final long serialVersionUID = 0xfffffffebd50f49fL;
	private Pager page;
	private List attenddailyList;
	private String emp;
	private List departs;
	private List locations;
	private List empType;
	private Employee employee;
	private String attendDate;
	private String attendDateTo;
	private Integer searchType;
	private Date requireDateFrom;
	private Date requireDateTo;
	private String searchOrExport;
	private String outmatchModelId;
	public static final String outputIoName = "OAttendDaily";
	private FileInputStream docStream;
	private String contentDisposition;
	private String serverFileName;
	private String searchMode;
	private IAttenddailyBO attenddailyBO;
}
